/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { LegacyModuleModelImpl } from '../../model/module/legacy-module-model-impl.js';
import { ConfigJson } from '../../options/configure/config-json-options.js';
import { OhosLogger } from '../../utils/log/ohos-logger.js';
import { TaskNames } from '../common/task-names.js';
import { ModuleTargetData } from '../data/hap-task-target-data.js';
import { TargetTaskService } from '../service/target-task-service.js';

import { LegacyMergeProfile } from './legacy-merge-profile.js';
import LegacyFATask = TaskNames.LegacyFATask;
import { FileSet } from '@ohos/hvigor';

import { DeviceTypeConst } from '../../const/device-type-const.js';
import { LegacyTargetSourceSetModel } from '../../model/source-set/source-set-model.js';
import { AbstractMakePackInfo } from '../abstract-make-pack-info.js';

/**
 * 生成老模型hap级别的pack.info
 */
export class LegacyMakePackInfo extends AbstractMakePackInfo {
  private log: OhosLogger = OhosLogger.getLogger(LegacyMakePackInfo.name);

  declareInputFiles(): FileSet {
    const targetSourceSetModel: LegacyTargetSourceSetModel = this.targetService.getTargetData().getModuleSourceSetModel() as LegacyTargetSourceSetModel;
    const targetRes = targetSourceSetModel.getLegacyModuleTargetRes();
    const targetJsonPath = targetRes.getJsonPath();

    return new FileSet().addEntry(targetJsonPath);
  }

  constructor(taskService: TargetTaskService) {
    super(taskService, LegacyFATask.MAKE_PACK_INFO);
  }

  initTaskDepends() {
    this.declareDepends(LegacyMergeProfile.name);
  }

  protected doTaskAction(): void {
    const configObj: ConfigJson.ConfigOptObj = this.moduleModel.getJsonObjByTargetName(this.targetName) as ConfigJson.ConfigOptObj;

    const appObj: object = {
      bundleName: configObj.app.bundleName,
      version: configObj.app.version,
    };

    this.doMakePackInfo(this.log, appObj, configObj.app.apiVersion);
  }

  getModuleObj(deviceTypes: string[], moduleModel: LegacyModuleModelImpl, apiVersion: ConfigJson.ApiVersionObj | undefined, targetName: string): object {
    const configObj = moduleModel.getJsonObjByTargetName(targetName);
    return {
      mainAbility: configObj.module.mainAbility,
      deviceType: deviceTypes,
      abilities: this.processAbilities(configObj.module.abilities),
      distro: configObj.module.distro,
      apiVersion: apiVersion ?? configObj.app.apiVersion,
    };
  }

  protected getPackageObj(targetData: ModuleTargetData, configObj: ConfigJson.ConfigOptObj, noSuffixHapName: string, noSuffixLiteHapName?: string): object[] {
    if (this.targetData.isSingleDeviceTypeTarget()) {
      return [
        {
          deviceType: targetData.getTargetDeviceType(),
          moduleType: configObj.module.distro.moduleType,
          deliveryWithInstall: configObj.module.distro.deliveryWithInstall,
          name: noSuffixHapName,
        },
      ];
    }

    const liteDeviceTypes = this.targetData.getTargetDeviceType().filter((deviceType) => DeviceTypeConst.LITE_DEVICES.includes(deviceType));

    const richDeviceTypes = this.targetData.getTargetDeviceType().filter((deviceType) => !DeviceTypeConst.LITE_DEVICES.includes(deviceType));

    return [
      {
        deviceType: richDeviceTypes,
        moduleType: configObj.module.distro.moduleType,
        deliveryWithInstall: configObj.module.distro.deliveryWithInstall,
        name: noSuffixHapName,
      },
      {
        deviceType: liteDeviceTypes,
        moduleType: configObj.module.distro.moduleType,
        deliveryWithInstall: configObj.module.distro.deliveryWithInstall,
        name: noSuffixLiteHapName,
      },
    ];
  }
}
